package com.jiutong.meipengdai.views.popwindow;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.jiutong.meipengdai.R;
import com.jiutong.meipengdai.base.CommonAdapter;
import com.jiutong.meipengdai.base.ViewHolder;
import com.jiutong.meipengdai.entity.CityListRespond;
import com.jiutong.meipengdai.http.present.CommonPresent;
import com.jiutong.meipengdai.http.subscribers.SubscriberOnNextListener;
import com.jiutong.meipengdai.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Callback;

public class CitySelectPopupWindow extends BasePopupWindow {
    private List<CityListRespond.DataBean> dataList;
    private List<CityListRespond.DataBean> cityList;//城市列表
    private List<CityListRespond.DataBean> areaList;//小区列表
    private List<CityListRespond.DataBean> streedList;//街道列表
    private HashMap<String, CityListRespond.DataBean> hashMap;
    private TextView tvDesc;
    private int currentLevel = 1;//当前list所处第几级
    private CiteSelectAdapter citySelectAdapter;
    private PopCallBack mCallBack;

    public CitySelectPopupWindow(Context context) {
        super(context);

    }

    @Override
    public void initView() {
        ListView listView = (ListView) mContainer.findViewById(R.id.list_view);
        tvDesc = (TextView) mContainer.findViewById(R.id.tv_desc);
        setTouchBoundDismiss(false);
        setAnimationStyle(R.style.dialog_animation_fade);
        setFocusable(false);
        dataList = new ArrayList<>();
        hashMap = new HashMap<>();

        citySelectAdapter = new CiteSelectAdapter(mContext, dataList, R.layout.item_city_select);
        listView.setAdapter(citySelectAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                HashMap<String, Object> fields = new HashMap<>();
                fields.put("id", dataList.get(position).getAreaCode());
                fields.put("resultAreaType", dataList.get(position).getAreaType() + 1 + "");
                CommonPresent.getAreaAndStreedList(mContext, new SubscriberOnNextListener<CityListRespond>() {
                    @Override
                    public void onNext(CityListRespond respond) {

                        if ("200".equals(respond.getResCode())) {


                            if (currentLevel == 1) {
                                hashMap.put("level_one", dataList.get(position));
                            } else if (currentLevel == 2) {
                                hashMap.put("level_two", dataList.get(position));
                            } else if (currentLevel == 3) {
                                hashMap.put("level_three", dataList.get(position));
                            }

                            tvDesc.setVisibility(View.VISIBLE);
                            tvDesc.setText(dataList.get(position).getAreaName());

                            if(respond.getData() == null || respond.getData().size() < 1 || currentLevel >= 3){
//                                ToastUtil.shortToast(mContext, "地区暂未开放，敬请期待！");
                                mCallBack.callBack(hashMap, cityList, areaList, streedList);
                                dismiss();
                                return;
                            }

                            currentLevel++;
                            dataList.clear();
                            if (currentLevel == 2) {
                                areaList = respond.getData();
                                dataList.addAll(areaList);
                            } else if (currentLevel == 3) {
                                streedList = respond.getData();
                                dataList.addAll(streedList);
                            }
                            citySelectAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }
                }, fields);
            }
        });

        loadData();
    }

    @Override
    protected void loadData() {
        CommonPresent.getCityData(mContext, new SubscriberOnNextListener<CityListRespond>() {
            @Override
            public void onNext(CityListRespond respond) {
                if ("200".equals(respond.getResCode())) {
                    cityList = respond.getData();
                    dataList.clear();
                    dataList.addAll(cityList);
                    citySelectAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.shortToast(mContext, respond.getResDesc());
                }
            }

            @Override
            public void onError(Throwable e) {

            }
        }, null);
    }

    @Override
    public void initEvent() {
    }

    @Override
    public View getContentView() {
        return mContainer.findViewById(R.id.pop);
    }

    @Override
    public int getLayoutId() {
        return R.layout.pop_city_select;
    }

    /**
     * 列表返回上一级
     */
    public void backUp() {
        if (currentLevel == 1) {
            dismiss();
        } else if (currentLevel == 2) {
            dataList.clear();
            dataList.addAll(cityList);
            tvDesc.setVisibility(View.GONE);
            citySelectAdapter.notifyDataSetChanged();
        } else if (currentLevel == 3) {
            dataList.clear();
            dataList.addAll(areaList);
            tvDesc.setText(hashMap.get("level_one").getAreaName());
            citySelectAdapter.notifyDataSetChanged();
        }
        currentLevel--;
    }

    public void setCallBack(PopCallBack callBack) {
        this.mCallBack = callBack;
    }

    public interface PopCallBack {
        void callBack(HashMap<String, CityListRespond.DataBean> map, List<CityListRespond.DataBean> cityList, List<CityListRespond.DataBean> zoneList, List<CityListRespond.DataBean> streetList);

        void dimissCallBack();
    }

    @Override
    public void dismiss() {
        mCallBack.dimissCallBack();
        super.dismiss();
    }

    public class CiteSelectAdapter extends CommonAdapter<CityListRespond.DataBean> {

        public CiteSelectAdapter(Context context, List<CityListRespond.DataBean> listDatas, int layoutId) {

            super(context, listDatas, layoutId);
        }

        @Override
        protected void fillData(ViewHolder holder, int position) {
            TextView tv_city = holder.getView(R.id.tv_city);
            tv_city.setText(listDatas.get(position).getAreaName());
        }
    }
}
